//
//  ReplacePwdVC.m
//  Recycling
//
//  Created by jzd on 2018/5/18.
//  Copyright © 2018年 宋佳. All rights reserved.
//

#import "ReplacePwdVC.h"

@interface ReplacePwdVC ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topCons;
@property (weak, nonatomic) IBOutlet UITextField *oldPwd;
@property (weak, nonatomic) IBOutlet UIView *oneLine;
@property (weak, nonatomic) IBOutlet UITextField *firstPwd;
@property (weak, nonatomic) IBOutlet UIView *twoLine;
@property (weak, nonatomic) IBOutlet UITextField *secondPwd;
@property (weak, nonatomic) IBOutlet UIView *threeLine;


@end

@implementation ReplacePwdVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BGCOLOR;
    self.navigationItem.title = @"修改密码";
    self.secondPwd.delegate = self;
    self.firstPwd.delegate = self;
    self.oldPwd.delegate = self;
    self.topCons.constant = statsBarHeight+NavgationBarH;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.oldPwd becomeFirstResponder];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.view endEditing:YES];
    [[SJNetWorkTool sharedTool].tasks makeObjectsPerformSelector:@selector(cancel)];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
- (IBAction)sureReplacePwd:(id)sender {
    [self.view endEditing:YES];
    
    if (self.oldPwd.text.length == 0) {
        [self.navigationController.view showToast:@"请输入原始密码" completion:nil];
        return;
    }
    if (self.firstPwd.text.length == 0) {
        [self.navigationController.view showToast:@"请输入新密码" completion:nil];
        return;
    }
    if (self.secondPwd.text.length == 0) {
        [self.navigationController.view showToast:@"请确认新密码" completion:nil];
        return;
    }
    if (![self.firstPwd.text isEqualToString:self.secondPwd.text]) {
        [self.navigationController.view showToast:@"两次新密码不一致" completion:nil];
        return;
    }
    
    if (![[SJMD5Tool MD5ForLower32Bate:self.oldPwd.text] isEqualToString:[UserInfoTool shareUserInfoTool].Info.MD5Pwd]) {
        [self.navigationController.view showToast:@"原始密码错误" completion:nil];
        return;
    }
    
    NSDictionary *params = @{@"uid":@([UserInfoTool shareUserInfoTool].Info.ID),
                             @"newpwd":[SJMD5Tool MD5ForLower32Bate:self.firstPwd.text]
                             };
    [[SJNetWorkTool sharedTool]requestWithURLString:changePwd parameters:params method:POST callBack:^(id responseObject, NSError *error) {
        if (responseObject) {
            int code = [[responseObject objectForKey:@"Code"] intValue];
            NSString *msg = [responseObject objectForKey:@"ErrorMessage"];
            if (code == 1) {
                UserInfo *user = [NSKeyedUnarchiver unarchiveObjectWithFile:[UserInfoTool shareUserInfoTool].savePath];
                user.MD5Pwd = [SJMD5Tool MD5ForLower32Bate:self.firstPwd.text];
                [NSKeyedArchiver archiveRootObject:user toFile:[UserInfoTool shareUserInfoTool].savePath];
                [UserInfoTool shareUserInfoTool].Info = user;
                [self.navigationController.view showToast:@"修改密码成功" completion:nil];
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                [self.navigationController.view showToast:msg completion:nil];
            }
        }
    }];
    
    
    
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.view endEditing:YES];
    SJLog(@"修改密码");
    [self sureReplacePwd:nil];
    return YES;
}
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    if (textField == self.oldPwd) {
        self.oneLine.backgroundColor = TITLECOLOR;
    }else if (textField == self.firstPwd){
        self.twoLine.backgroundColor = TITLECOLOR;
    }else if (textField == self.secondPwd){
        self.threeLine.backgroundColor = TITLECOLOR;
    }
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField == self.oldPwd) {
        self.oneLine.backgroundColor = [UIColor lightGrayColor];
    }else if (textField == self.firstPwd){
        self.twoLine.backgroundColor = [UIColor lightGrayColor];
    }else if (textField == self.secondPwd){
        self.threeLine.backgroundColor = [UIColor lightGrayColor];
    }
}
@end
